package com.hugo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hugo.eduservice.entity.EduChapter;
import com.hugo.eduservice.entity.EduVideo;
import com.hugo.eduservice.entity.chapter.ChapterVo;
import com.hugo.eduservice.entity.chapter.VideoVo;
import com.hugo.eduservice.mapper.EduChapterMapper;
import com.hugo.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hugo.eduservice.service.EduVideoService;
import com.hugo.serviceconfig.exceptionhandler.HugoException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hugo
 * @since 2020-09-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getTreeChapter(String courseId) {
        //根据课程id查询章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduChapter> eduChaptersList = baseMapper.selectList(wrapper);

        //查询小节
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(queryWrapper);

        List<ChapterVo> finalList = new ArrayList<>();
        //遍历List进行封装
        for (int i = 0; i < eduChaptersList.size(); i++) {
            EduChapter eduChapter = eduChaptersList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            //给chaptervo赋值
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);

            List<VideoVo> videoVos = new ArrayList<>();
            for (int a = 0; a < eduVideoList.size(); a++) {
               EduVideo eduVideo = eduVideoList.get(a);
               if (eduVideo.getChapterId().equals(eduChapter.getId())){
                   VideoVo videoVo = new VideoVo();
                   BeanUtils.copyProperties(eduVideo,videoVo);
                   videoVos.add(videoVo);

               }
            }
            //封装之后的小结List集合 放到章节对象里面
            chapterVo.setChildren(videoVos);

        }


        //遍历进行封装
        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(queryWrapper);
        if (count>0){
            throw new HugoException(20001,"不能删除");
        }else {
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }

    }

    @Override
    public void removeChapterById(String courseId) {
        QueryWrapper<EduChapter> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.delete(queryWrapper);
    }
}
